package yong.birthdayreminder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class EditBuddyActivity extends Activity implements OnClickListener{
	
    private BirthdayReminderDBHelper dbhelper = null;
    private SQLiteDatabase db = null;
    private String SQLUpdate = null;
    private String SQLDelete = null;
	private Button btn_delete;
	private Button btn_update;
	private Button btn_return;
	private EditText v_name;
	private EditText v_hobby;
	private DatePicker v_birthday;
	private long _id;
	private String name;
	private String hobby;
	private String birthday;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dbhelper = new BirthdayReminderDBHelper(getBaseContext());
        db = dbhelper.getWritableDatabase();
		this.setContentView(R.layout.editbuddy);
		btn_delete = (Button)findViewById(R.id.editbuddy_btn_delete);
		btn_update = (Button)findViewById(R.id.editbuddy_btn_update);
		btn_return = (Button)findViewById(R.id.editbuddy_btn_return);
		v_name = (EditText)findViewById(R.id.editbuddy_editetext_name);
		v_hobby = (EditText)findViewById(R.id.editbuddy_editetext_hobby);
		v_birthday = (DatePicker)findViewById(R.id.editbuddy_datepicker_birthday);
		Bundle bundle = this.getIntent().getExtras();
		_id = bundle.getLong("_id");
		name = (String)bundle.get("name");
		hobby = (String)bundle.get("hobby");
		birthday = (String)bundle.get("birthday");
		v_name.setText(name);
		v_hobby.setText(hobby);
		//Adapt the birthday
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();

		try {
			sdf.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		c = sdf.getCalendar();
		v_birthday.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
		
		btn_delete.setOnClickListener(this);
		btn_update.setOnClickListener(this);
		btn_return.setOnClickListener(this);	
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.editbuddy_btn_update) {
			int month = v_birthday.getMonth()+1;
			int day = v_birthday.getDayOfMonth();
			int year = v_birthday.getYear();
			String month_s = null;
			String day_s = null;
			if (month < 10 ) {
				month_s = "0"+month;
			}else {
				month_s = ""+month;
			}
			if (day < 10) {
				day_s = "0" + day;
			}else {
				day_s = "" + day;
			}
			
			ContentValues cv = new ContentValues();
			cv.put("name", v_name.getText().toString());
			cv.put("hobby", v_hobby.getText().toString());
	        cv.put("birthday", year+"-"+month_s+"-"+day_s);		
			db.update("birthday", cv, "_id=?", new String[]{_id+""});
			this.finish();
			
		}else if (id == R.id.editbuddy_btn_delete) {
			db.delete("birthday", "_id=?", new String[]{_id+""});
			//Clear the alarm
			AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); 
			PendingIntent pendingIntent = PendingIntent.getActivity(this, (int)_id, null, 0);
			am.cancel(pendingIntent);
			this.finish();
			
		}else if (id == R.id.editbuddy_btn_return) {
			this.finish();
		}
		// TODO Auto-generated method stub
		
	}

}
